import { defineStore } from 'pinia';
import { ref } from 'vue';
import { translateToHK } from '@/utils/translateToHK';
import { translateToTW } from '@/utils/translateToTW';
import { translateToEN } from '@/utils/translateToEN';

export const useLanguageStore = defineStore('language', () => {
  const currentLanguage = ref('中国大陆版');
  
  // 获取翻译函数
  const getTranslator = () => {
    const translators = {
      '香港版': translateToHK,
      '台湾版': translateToTW,
      '国际版': translateToEN,
      '中国大陆版': (text) => text
    };
    return translators[currentLanguage.value];
  };

  // 翻译文本
  const translate = (text) => {
    // 添加安全检查
    if (!text || typeof text !== 'string') {
      return text || '';
    }
    
    const translator = getTranslator();
    return translator(text);
  };

  // 切换语言
  const setLanguage = (language) => {
    currentLanguage.value = language;
  };

  return {
    currentLanguage,
    translate,
    setLanguage
  };
});